import { Node } from '@/sources/LinkedList/singleLinkList'
/**
 * 判断链表是否有环
 * 方法: 使用不同的步长
 */
export const hasCycle = <T>(head: Node<T> | null) => {
    if (head === null || head.next === null) return false;
    let one = head, two = head.next.next;
    while (one !== null && two !== null) {
        if (one === two) {
            return true
        }
        one = one?.next!
        two = two?.next?.next!
    }
    return false
}

export const hasCycle2 = <T>(head: any) => {
    if (head === null || head.next === null) return false;
    while (head) {
        head.count = head.count || 0
        if (head.count === 2) return true
        head.count++
        head = head.next;
    }
    return false
}